﻿//========================================================================//
//      Copyright 2012, Smilodon Studio Inc.
//      All rights reserved.
//========================================================================//

/**
 *	base 
 *      @author: CYS
 */


var canvas;
var g2D;

var height;
var width;
var fullWindow = true;
var ratio = 1;

var time = 0;
var dtime = 0;
var startime;
var lasttime;
var maxLineNum = 50;
var lineMouse;
var lines = new Array(maxLineNum);
var lineColor = 'rgba(255, 255, 225, 1)';

function canvasResize()
{
	ratio = window.innerWidth/canvas.width;
	canvas.width = window.innerWidth;
	canvas.height = window.innerHeight;
	width = canvas.width;
	height = canvas.height;
	if(!g2D)g2D = canvas.getContext("2d");
	g2D.lineWidth = 100;
	g2D.lineCap = 'round';
	g2D.strokeStyle = lineColor;
	g2D.fillStyle   = lineColor;

}

function init() 
{
	canvas = document.getElementById('canvas2D');
	canvasResize();

	var touchable = 'createTouch' in document;
	if (touchable)
	{
		canvas.addEventListener('touchstart', onTouchStart, false);
		canvas.addEventListener('touchmove', onTouchMove, false);
		canvas.addEventListener('touchend', onTouchEnd, false);
	}
	else
	{
		canvas.addEventListener('mousedown', onTouchStart, false);
		canvas.addEventListener('mousemove', onTouchMove, false);
		canvas.addEventListener('mouseup', onTouchEnd, false);
	}

	canvas.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );

	for(var i = 0; i < lines.length; i++) 
	{
		var p = {};
		p.x = -100;
		p.y = -100;
		p.down = false;
		lines[i]  = p;
	}
	var p = {};
	p.x = -100;
	p.y = -100;
	p.down = false;
	lineMouse = p;

}

var bTimeout = false;
window.onresize = function()
{
	if (fullWindow) canvasResize();
	bTimeout = true;
}

var lastOrientation;
var first = true;
window.ondeviceorientation =  function(e) 
{	
	if (first) 
	{
		lastOrientation = window.orientation;
		first = false;
	}
	if(lastOrientation !== window.orientation)
	{
		window.onresize();
		lastOrientation = window.orientation;
	}
}

window.onblur = function()
{
	bTimeout = true;
}


function drawScene()
{
	// draw background color 
	//g2D.clearRect(0, 0, canvas.width,canvas.height);
	g2D.globalAlpha = 0.025;
	g2D.fillStyle = '#000';
	g2D.fillRect(0, 0, canvas.width,canvas.height);
	
	g2D.save();
	
	g2D.globalAlpha = 1;
	g2D.fillStyle = lineColor;
	g2D.shadowOffsetX = 0;
	g2D.shadowOffsetY = 0;
	g2D.shadowBlur    = 16;
	g2D.shadowColor   = lineColor;
	for(var i = 0; i < lines.length; i++) 
	{
		if(lines[i].down)
		{
			drawPoint(lines[i]);
			lines[i].down = false;
		}
	}
	
	if(lineMouse.down)
	{
		drawPoint(lineMouse);
	}

	g2D.restore();
}


function drawPoint(p)
{
	g2D.beginPath();
	g2D.arc(p.x, p.y, g2D.lineWidth/2, 0, 2*Math.PI, true );
	g2D.closePath();
	g2D.fill();
}

function onTouchStart(event)
{
	// Prevent the browser from doing its default thing (scroll, zoom)
	event.preventDefault();


	if(event.clientX) // on mouse down
	{
		var p = {};
		p.x = event.clientX;
		p.y = event.clientY;
		p.down = true;
		
		lineMouse = p;

	}
	else	// on touch star
	{

		for(var i = 0; i < event.touches.length; i++)
		{
			var p = {};
			p.x = event.touches[i].clientX;
			p.y = event.touches[i].clientY;
			p.down = true;
			lines[i] = p;
		}
	}

}

function onTouchMove(event)
{
	event.preventDefault();

	if(event.clientX) // on mouse move
	{
		var p = {};
		p.x = event.clientX;
		p.y = event.clientY;

		lineMouse.x = p.x;
		lineMouse.y = p.y;
	}
	
	if(!event.touches) return;
	
	for(var i = 0; i < event.touches.length; i++)
	{
		lines[i].x = event.touches[i].clientX;
		lines[i].y = event.touches[i].clientY;
		lines[i].down = true;
	}


}

function onTouchEnd(event)
{
	event.preventDefault();

	if(event.clientX) // on mouse up
	{
		lineMouse.down = false;
	}
	if(!event.touches) return;

	for(var i = 0; i < event.touches.length; i++)
	{
		lines[i].x = event.touches[i].clientX;
		lines[i].y = event.touches[i].clientY;
		lines[i].down = true;
	}

}



function gameloop() 
{
	window.requestAnimFrame( gameloop );
	if(bTimeout)
	{
		lasttime = Date.now();
		bTimeout = false;
	}
	dtime = Date.now() - lasttime;
	time = Date.now() - startime;
	drawScene();
	lasttime = Date.now();

}


function game()
{
	init();
	gameloop();
}




